package com.xmb.wechat.baserecyclerview;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmb.wechat.R;
import com.xmb.wechat.widget.CustomLoadMoreView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

/**
 * @explain Created by lzp on 2017/5/17.
 */

public abstract class BaseRecyclerViewManager<T, k extends com.chad.library.adapter.base.BaseViewHolder> extends RecyclerView.OnScrollListener {
    BaseQuickAdapter<T, k> adapter;
    RecyclerView           recyclerView;
    protected Context context;

    public BaseRecyclerViewManager(Activity activity) {
        recyclerView = activity.findViewById(R.id.recyclerView);
        context = activity;
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(Fragment fragment) {
        recyclerView = fragment.getView().findViewById(R.id.recyclerView);
        context = fragment.getContext();
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(Fragment fragment, @IdRes int resId) {
        recyclerView = fragment.getView().findViewById(resId);
        context = fragment.getContext();
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(Activity activity, @IdRes int resId) {
        recyclerView = activity.findViewById(resId);
        context = activity;
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);
        context = view.getContext();
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(View view, @IdRes int resId) {
        recyclerView = view.findViewById(resId);
        context = view.getContext();
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    public BaseRecyclerViewManager(Context context, RecyclerView recyclerView) {
        this.context = context;
        this.recyclerView = recyclerView;
        adapter = initAdapter();
        openAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setLayoutManager(getLayoutManager());
        if (getItenDecoration() != null)
            recyclerView.addItemDecoration(getItenDecoration());
        adapter.bindToRecyclerView(recyclerView);
        recyclerView.setItemAnimator(null);
    }

    protected void openAnimation(int animation) {
        adapter.openLoadAnimation(animation);
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
    }

    protected RecyclerView.ItemDecoration getItenDecoration() {
        return new HorizontalDividerItemDecoration.Builder(context).size(SizeUtils.dp2px(0.5f)).color(ContextCompat.getColor(context, R.color.black_02)).build();
    }

    public abstract BaseQuickAdapter<T, k> initAdapter();

    public void addData(List<T> data) {
        adapter.addData(data);
    }

    public void addData(int position, List<T> data) {
        adapter.addData(position, data);
    }

    public void additem(T data) {
        adapter.addData(data);
    }

    public void removeItem(int position) {
        adapter.remove(position);
    }

    public void replaceData(List<T> data) {
        adapter.replaceData(data);
    }

    public void setOnItemClick(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        adapter.setOnItemClickListener(onItemClickListener);
    }

    public BaseQuickAdapter getAdapter() {
        return adapter;
    }

    public List<T> getData() {
        return adapter.getData();
    }

    public void clearData() {
        List<T> datas = getData();
        if (datas != null) {
            datas.clear();
            adapter.notifyDataSetChanged();
        }
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void setLoadMoreEnable(boolean enable) {
        adapter.setEnableLoadMore(enable);
    }

    public void loadMoreComplete() {
        adapter.loadMoreComplete();
    }

    public void loadMoreFail() {
        adapter.loadMoreFail();
    }

    public void loadMoreEnd(boolean... gone) {
        adapter.loadMoreEnd(gone.length > 0 ? gone[0] : false);
    }

    protected CustomLoadMoreView getLoadMoreView() {
        return new CustomLoadMoreView();
    }

    public void setEmptyView(View view) {
        adapter.setEmptyView(view);
    }

    public void addOnLoadMoreListener(BaseQuickAdapter.RequestLoadMoreListener loadMoreLisenter) {
        adapter.setLoadMoreView(getLoadMoreView());
        adapter.setOnLoadMoreListener(loadMoreLisenter, getRecyclerView());
    }

    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     */
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        Log.i("recyclerView", "onScrollStateChanged");
    }

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     * <p>
     * This callback will also be called if visible item range changes after a layout
     * calculation. In that case, dx and dy will be 0.
     *
     * @param recyclerView The RecyclerView which scrolled.
     * @param dx           The amount of horizontal scroll.
     * @param dy           The amount of vertical scroll.
     */
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        Log.i("recyclerView", "onScrolled");
    }
}
